f778b2
@@ -16,12 +16,14 @@
 package org.springframework.data.mongodb.config;
 
 import java.beans.PropertyEditorSupport;
+import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.HashSet;
 import java.util.Set;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.util.Assert;
 import org.springframework.util.StringUtils;
 
 import com.mongodb.ServerAddress;
@@ -35,6 +37,11 @@
import com.mongodb.ServerAddress;
  */
 public class ServerAddressPropertyEditor extends PropertyEditorSupport {
 
+	/**
+	 * A port is a number without a leading 0 at the end of the address that is proceeded by just a single :.
+	 */
+	private static final String HOST_PORT_SPLIT_PATTERN = "(?<!:):(?=[123456789]\\d*$)";
+	private static final String COULD_NOT_PARSE_ADDRESS_MESSAGE = "Could not parse address {} '{}'. Check your replica set configuration!";
 	private static final Logger LOG = LoggerFactory.getLogger(ServerAddressPropertyEditor.class);
 
 	/*
@@ -77,22 +84,53 @@
public class ServerAddressPropertyEditor extends PropertyEditorSupport {
 	 */
 	private ServerAddress parseServerAddress(String source) {
 
-		String[] hostAndPort = StringUtils.delimitedListToStringArray(source.trim(), ":");
+		if (!StringUtils.hasText(source)) {
+			LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "source", source);
+			return null;
+		}
+
+		String[] hostAndPort = extractHostAddressAndPort(source.trim());
 
-		if (!StringUtils.hasText(source) || hostAndPort.length > 2) {
-			LOG.warn("Could not parse address source '{}'. Check your replica set configuration!", source);
+		if (hostAndPort.length > 2) {
+			LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "source", source);
 			return null;
 		}
 
 		try {
-			return hostAndPort.length == 1 ? new ServerAddress(hostAndPort[0]) : new ServerAddress(hostAndPort[0],
-					Integer.parseInt(hostAndPort[1]));
+			InetAddress hostAddress = InetAddress.getByName(hostAndPort[0]);
+			Integer port = hostAndPort.length == 1 ? null : Integer.parseInt(hostAndPort[1]);
+
+			return port == null ? new ServerAddress(hostAddress) : new ServerAddress(hostAddress, port);
 		} catch (UnknownHostException e) {
-			LOG.warn("Could not parse host '{}'. Check your replica set configuration!", hostAndPort[0]);
+			LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "host", hostAndPort[0]);
 		} catch (NumberFormatException e) {
-			LOG.warn("Could not parse port '{}'. Check your replica set configuration!", hostAndPort[1]);
+			LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "port", hostAndPort[1]);
 		}
 
 		return null;
 	}
+
+	/**
+	 * Extract the host and port from the given {@link String}.
+	 * 
+	 * @param addressAndPortSource must not be {@literal null}.
+	 * @return
+	 */
+	private String[] extractHostAddressAndPort(String addressAndPortSource) {
+
+		Assert.notNull(addressAndPortSource, "Address and port source must not be null!");
+
+		String[] hostAndPort = addressAndPortSource.split(HOST_PORT_SPLIT_PATTERN);
+		String hostAddress = hostAndPort[0];
+
+		if (isHostAddressInIPv6BracketNotation(hostAddress)) {
+			hostAndPort[0] = hostAddress.substring(1, hostAddress.length() - 1);
+		}
+
+		return hostAndPort;
+	}
+
+	private boolean isHostAddressInIPv6BracketNotation(String hostAddress) {
+		return hostAddress.startsWith("[") && hostAddress.endsWith("]");
+	}
 }
